﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using FsAddInManager.Properties;

namespace FsAddInManager
{
    public partial class MainForm : Form
    {
        private AIM m_aim = null; //占位

        private SplitButton saveSplitButton;
        private Tree commandsTreeView;
        private Tree applicationsTreeView;

        private int _bToRefreshTree = 0;
        //硬编码:如果为0则文本框继续刷新树. 如果为1则不刷新树
        //文本框的更改,是否刷新到树状结构

        public MainForm(AIM aim)
        {
            m_aim = aim;
            InitializeComponent();
            InitialUiMan();

            Text = @"飞思-" + Resources.AppName; //标特
        }


        public void InitialUiMan()
        {
            {
                saveSplitButton = new SplitButton();
                saveSplitButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
                saveSplitButton.AutoSize = true;
                saveSplitButton.ContextMenuStrip = saveContextMenuStrip;
                saveSplitButton.Location = btnTmpSave.Location;

                saveSplitButton.Size = btnTmpSave.Size;
                saveSplitButton.Font = btnTmpSave.Font;
                saveSplitButton.ForeColor = btnTmpSave.ForeColor;
                saveSplitButton.Name = "saveSplitButton";
                saveSplitButton.SplitMenuStrip = saveContextMenuStrip;
                saveSplitButton.TabIndex = 115;

                saveSplitButton.Text = btnTmpSave.Text;
                saveSplitButton.TextAlign = ContentAlignment.MiddleRight;
                saveSplitButton.UseVisualStyleBackColor = true;
                saveSplitButton.Click += new EventHandler(saveSplitButton_Click);
                Controls.Add(saveSplitButton);

                btnTmpSave.Enabled = false;
                btnTmpSave.Visible = false;
                Controls.Remove(btnTmpSave);
            }

            {
                commandsTreeView = new Tree();
                commandsTreeView.ActiveNode = null;
                commandsTreeView.Dock = DockStyle.Fill;
                commandsTreeView.Location = tmpTreeViewCmd.Location;
                commandsTreeView.Name = "commandsTreeView";
                commandsTreeView.Size = tmpTreeViewCmd.Size;
                commandsTreeView.TabIndex = 0;
                commandsTreeView.AfterSelect += new TreeViewEventHandler(commandsTreeView_AfterSelect);
                commandsTreeView.NodeMouseClick += new TreeNodeMouseClickEventHandler(commandsTreeView_NodeMouseClick);
                commandsTreeView.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(commandsTreeView_NodeMouseDoubleClick);
                commandsTreeView.Leave += new EventHandler(commandsTreeView_Leave);
                commandsTabPage.Controls.Add(commandsTreeView);

                tmpTreeViewCmd.Enabled = false;
                tmpTreeViewCmd.Visible = false;
                commandsTabPage.Controls.Remove(tmpTreeViewCmd);
            }

            {
                applicationsTreeView = new Tree();

                applicationsTreeView.ActiveNode = null;
                applicationsTreeView.Dock = DockStyle.Fill;
                applicationsTreeView.Location = tmpTreeViewApp.Location;
                applicationsTreeView.Name = "applicationsTreeView";
                applicationsTreeView.Size = tmpTreeViewApp.Size;
                applicationsTreeView.TabIndex = 0;
                applicationsTreeView.AfterSelect += new TreeViewEventHandler(applicationsTreeView_AfterSelect);
                applicationsTreeView.Click += new EventHandler(applicationsTreeView_Click);
                applicationsTreeView.Leave += new EventHandler(applicationsTreeView_Leave);
                applicationsTabPage.Controls.Add(applicationsTreeView);


                tmpTreeViewApp.Enabled = false;
                tmpTreeViewApp.Visible = false;
                applicationsTabPage.Controls.Remove(tmpTreeViewApp);
            }
        }


        public sealed override string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        private void loadButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = Resources.LoadFileFilter;
            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                ShowStatusError(Resources.LoadCancelled);
                return;
            }

            string fileName = openFileDialog.FileName;
            string empty = string.Empty;
            AddinType addinType = m_aim.AddinManager.LoadAddin(fileName);
            if (addinType == AddinType.Invalid)
            {
                ShowStatusError(Resources.LoadFailed + fileName);
                return;
            }

            ShowStatusLabel(Resources.LoadSucceed + fileName);
            m_aim.AddinManager.SaveToAimIni();


            commandsTreeView_RefreshData(false); //刷新命令表
            applicationsTreeView_RefreshData(); //刷新程序表

            switch (addinType)
            {
                case AddinType.Command:
                case AddinType.Mixed:
                    externalToolsTabControl.SelectTab(commandsTabPage);
                    commandsTreeView.Focus();
                    break;
                case AddinType.Application:
                    externalToolsTabControl.SelectTab(applicationsTabPage);
                    applicationsTreeView.Focus();
                    break;
            }

            notesTextBox.Text = empty;
            removeButton_RefreshData();
        }

        private void applicationsTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag == null)
            {
                return;
            }

            Addin addin = e.Node.Tag as Addin;
            if (addin != null)
            {
                m_aim.ActiveApp = addin;
                notesTextBox.Text = m_aim.ActiveApp.FilePath;
            }

            AddinItem addinItem = e.Node.Tag as AddinItem;
            if (addinItem != null)
            {
                Addin addin2 = e.Node.Parent.Tag as Addin;
                if (addin2 != null)
                {
                    m_aim.ActiveApp = addin2;
                    m_aim.ActiveAppItem = addinItem;
                    notesTextBox.Text = m_aim.ActiveApp.FilePath;
                }
            }

            DisableControl();
            removeButton_RefreshData();
        }

        private void applicationsTreeView_Click(object sender, EventArgs e)
        {
            removeButton_RefreshData();
        }

        private void applicationsTreeView_Leave(object sender, EventArgs e)
        {
            DisableControl();
            if (!removeButton.Focused)
            {
                removeButton.Enabled = false;
            }
        }

        private void DisableControl()
        {
            /*nametextBox.Enabled = false;
            nametextBox.Text = "";*/

            descriptionTextBox.Text = "";
            descriptionTextBox.Enabled = false;
            runButton.Enabled = false;
        }

        private void externalToolsTabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (1 == externalToolsTabControl.SelectedIndex)
            {
                //禁用控件
                {
                    nametextBox.Enabled = false;
                    nametextBox.Text = "";

                    descriptionTextBox.Text = "";
                    descriptionTextBox.Enabled = false;
                    runButton.Enabled = false;
                }

                applicationsTreeView.Focus();
                removeButton_RefreshData();
                return;
            }

            commandsTreeView.Focus();
            refreshData(false);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            commandsTreeView_RefreshData(true);
            applicationsTreeView_RefreshData();
            DisableControl();


            removeButton.Enabled = false;
            if (commandsTreeView.Nodes.Count != 0)
            {
                commandsTreeView.SelectedNode = commandsTreeView.Nodes[0];
                commandsTreeView.Nodes[0].EnsureVisible();

                //Zcb 2021-05-24 17:26
                {
                    nametextBox.Enabled = true;
                    nametextBox.Text = "";
                    nametextBox.Focus();
                }
            }
        }

        /// <summary>
        /// 刷新当前的命令及文本控件
        /// </summary>
        /// <param name="bClickTree">用户单击树结构导致的刷新</param>
        private void refreshData(bool bClickTree)
        {
            if (commandsTreeView.SelectedNode != null && commandsTreeView.SelectedNode.GetNodeCount(true) == 0)
            {
                if (m_aim.ActiveCmdItem != null)
                {
                    if (bClickTree)
                    {
                        nametextBox.Text = m_aim.ActiveCmdItem.Name;
                        nametextBox.Enabled = true;
                        descriptionTextBox.Text = m_aim.ActiveCmdItem.Description;
                    }
                }


                descriptionTextBox.Enabled = true;
                runButton.Enabled = true;
            }
            else
            {
                DisableControl();
            }

            removeButton_RefreshData();
        }

        private void runButton_Click(object sender, EventArgs e)
        {
            Run();
        }

        private void Run()
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            int num = 0;
            int num2 = 0;
            if (externalToolsTabControl.SelectedIndex == 0 && commandsTreeView.SelectedNode != null)
            {
                if (commandsTreeView.SelectedNode.GetNodeCount(true) != 0)
                {
                    num = commandsTreeView.SelectedNode.Index;
                    num2 = commandsTreeView.GetNodeCount(false) - 1;
                    if (num == num2)
                    {
                        num--;
                    }

                    m_aim.AddinManager.Commands.RemoveAddIn(m_aim.ActiveCmd);
                    m_aim.ActiveCmd = null;
                    m_aim.ActiveCmdItem = null;
                }
                else
                {
                    num = commandsTreeView.SelectedNode.Index;
                    num2 = commandsTreeView.GetNodeCount(false) - 1;
                    if (num == num2)
                    {
                        num--;
                    }

                    m_aim.ActiveCmd.RemoveItem(m_aim.ActiveCmdItem);
                    m_aim.ActiveCmd = null;
                    m_aim.ActiveCmdItem = null;
                }

                m_aim.AddinManager.SaveToAimIni();
            }

            if (1 == externalToolsTabControl.SelectedIndex && applicationsTreeView.SelectedNode != null)
            {
                if (applicationsTreeView.SelectedNode.GetNodeCount(true) != 0)
                {
                    num = applicationsTreeView.SelectedNode.Index;
                    num2 = applicationsTreeView.GetNodeCount(false) - 1;
                    if (num == num2)
                    {
                        num--;
                    }

                    m_aim.AddinManager.Applications.RemoveAddIn(m_aim.ActiveApp);
                }
                else
                {
                    num = applicationsTreeView.SelectedNode.Index;
                    num2 = applicationsTreeView.GetNodeCount(false) - 1;
                    if (num == num2)
                    {
                        num--;
                    }

                    m_aim.ActiveApp.RemoveItem(m_aim.ActiveAppItem);
                }

                m_aim.ActiveApp = null;
                m_aim.ActiveAppItem = null;
                m_aim.AddinManager.SaveToAimIni();
            }

            commandsTreeView_RefreshData(false);
            applicationsTreeView_RefreshData();
            if (externalToolsTabControl.SelectedIndex == 0)
            {
                if (num2 > 0)
                {
                    commandsTreeView.Focus();
                    commandsTreeView.SelectedNode = commandsTreeView.Nodes[num];
                }
                else
                {
                    notesTextBox.Text = " ";
                }
            }

            if (1 == externalToolsTabControl.SelectedIndex)
            {
                if (num2 > 0)
                {
                    applicationsTreeView.Focus();
                    applicationsTreeView.SelectedNode = applicationsTreeView.Nodes[num];
                }
                else
                {
                    notesTextBox.Text = " ";
                }
            }

            removeButton_RefreshData();
        }

        private void removeButton_Leave(object sender, EventArgs e)
        {
            removeButton_RefreshData();
        }

        private void removeButton_RefreshData()
        {
            if (commandsTreeView.Focused)
            {
                if (commandsTreeView.SelectedNode != null)
                {
                    removeButton.Enabled = true;
                    if (m_aim.ActiveCmd != null)
                    {
                        notesTextBox.Text = m_aim.ActiveCmd.FilePath;
                        return;
                    }

                    notesTextBox.Text = "";
                    return;
                }
                else
                {
                    removeButton.Enabled = false;
                    if (m_aim.ActiveCmd != null)
                    {
                        notesTextBox.Text = m_aim.ActiveCmd.FilePath;
                        return;
                    }

                    notesTextBox.Text = "";
                    return;
                }
            }
            else
            {
                if (!applicationsTreeView.Focused)
                {
                    removeButton.Enabled = false;
                    return;
                }

                if (applicationsTreeView.SelectedNode != null)
                {
                    removeButton.Enabled = true;
                    if (m_aim.ActiveApp != null)
                    {
                        notesTextBox.Text = m_aim.ActiveApp.FilePath;
                        return;
                    }

                    notesTextBox.Text = "";
                    return;
                }
                else
                {
                    removeButton.Enabled = false;
                    if (m_aim.ActiveApp != null)
                    {
                        notesTextBox.Text = m_aim.ActiveApp.FilePath;
                        return;
                    }

                    notesTextBox.Text = "";
                    return;
                }
            }
        }

        private void saveSplitButton_Click(object sender, EventArgs e)
        {
            if (m_aim.AddinManager.AppCount != 0 || m_aim.AddinManager.CmdCount != 0)
            {
                saveSplitButton.ShowContextMenuStrip();
            }
        }

        /// <summary>
        /// 当焦点离开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nametextBox_Leave(object sender, EventArgs e)
        {
            /*m_aim.ActiveCmdItem.Name = nametextBox.Text;
            m_aim.AddinManager.SaveToAimIni();*/

            //查找合适的Cmd
            /*{
                AddinsCommand addins = m_aim.AddinManager.Commands;
                AddinsCommand filiterEd = new AddinsCommand();
                {
                    foreach (KeyValuePair<string, Addin> keyValuePair in addins.AddinDict)
                    {
                        string key = keyValuePair.Key;
                        Addin addin = keyValuePair.Value;
                        addin.ItemList.Sort(new ItemComparer());

                        Addin filterAddin = new Addin(addin.FilePath);
                        foreach (AddinItem addinItem in addin.ItemList)
                        {
                            if (MathCmd(cmdName, addinItem.FullClassName))
                            {
                                filterAddin.ItemList.Add(addinItem);
                            }
                        }

                        filiterEd.AddAddIn(filterAddin);
                    }
                }
            }*/


            /*m_aim.ActiveCmdItem = GetNodeByTxt;

            */
        }

        private void nametextBox_TextChanged(object sender, EventArgs e)
        {
            if (1 == externalToolsTabControl.SelectedIndex)
            {
                return;
            }

            string cmdName = nametextBox.Text;
            cmdName = cmdName.Trim();
            runButton.Enabled = false;

            //如果文本改变的原因为: 用户输入
            if (sender == nametextBox)
            {
                AddinsCommand addins = m_aim.AddinManager.Commands;

                //1.如果为空
                if (string.IsNullOrWhiteSpace(cmdName))
                {
                    RefreshTreeView(commandsTreeView, addins, true);
                    return;
                }

                if (_bToRefreshTree == 0)
                {
                    //2.如果不为空,则重新刷新列表,并尝试找到节点
                    AddinsCommand filiterEd = new AddinsCommand();

                    foreach (KeyValuePair<string, Addin> keyValuePair in addins.AddinDict)
                    {
                        string key = keyValuePair.Key;
                        Addin addin = keyValuePair.Value;
                        addin.ItemList.Sort(new ItemComparer());

                        Addin filterAddin = new Addin(addin.FilePath);
                        foreach (AddinItem addinItem in addin.ItemList)
                        {
                            if (MathCmd(cmdName, addinItem.FullClassName))
                            {
                                filterAddin.ItemList.Add(addinItem);
                            }
                        }

                        filiterEd.AddAddIn(filterAddin);
                    }

                    RefreshTreeView(commandsTreeView, filiterEd, false);
                    commandsTreeView.ExpandAll();

                    //3.在树中尝试找到节点
                    var foundNode = commandsTreeView.GetNodeByTxt(cmdName);
                    if (foundNode != null)
                    {
                        commandsTreeView.SelectedNode = foundNode;
                        if (commandsTreeView.SelectedNode != null)
                        {
                            commandsTreeView.SelectedNode.EnsureVisible();
                        }

                        AddinItem cmdItem = foundNode.Tag as AddinItem;
                        if (cmdItem != null)
                        {
                            if (m_aim.ActiveCmdItem != null)
                            {
                                m_aim.ActiveCmdItem.Name = nametextBox.Text;
                                m_aim.AddinManager.SaveToAimIni();
                                runButton.Enabled = true;
                            }
                            else
                            {
                                Debug.Assert(false);
                            }
                        }
                    }
                }

                _bToRefreshTree = 0; //不管是否是0.总设置为0
            }
            else //其他原因导致文本改变
            {
#if DEBUG
                MessageBox.Show("非用户输入导致的文本改变!");
#endif //DEBUG
            }

            //runButton.Enabled = true;
            //#ToDo此时应该判断 文本框的命令是否能匹配到.
        }


        //执行正则表达式

        public static bool MathCmd(string regexString, string judgeStr)
        {
            if (!string.IsNullOrEmpty(regexString))
            {
                return Regex.IsMatch(judgeStr, regexString,
                    RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            }

            return true;
        }

        private void descriptionTextBox_Leave(object sender, EventArgs e)
        {
            m_aim.ActiveCmdItem.Description = descriptionTextBox.Text;
            m_aim.AddinManager.SaveToAimIni();
        }

        private void commandsTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag == null)
            {
                return;
            }

            Addin addin = e.Node.Tag as Addin;
            if (addin != null)
            {
                m_aim.ActiveCmd = addin;
                notesTextBox.Text = m_aim.ActiveCmd.FilePath;
            }

            AddinItem addinItem = e.Node.Tag as AddinItem;
            if (addinItem != null)
            {
                Addin addin2 = e.Node.Parent.Tag as Addin;
                if (addin2 != null)
                {
                    m_aim.ActiveCmd = addin2;
                    m_aim.ActiveCmdItem = addinItem;
                    notesTextBox.Text = m_aim.ActiveCmd.FilePath;
                }
            }

            bool bClick = e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard;

            if (bClick)
            {
                _bToRefreshTree = 1; //如果用户单击了树控件导致文本框的更改,文本框更改后,不会在刷新到tree了.否则就会嵌套了
            }

            refreshData(bClick);
        }

        private void commandsTreeView_Leave(object sender, EventArgs e)
        {
            if (!nametextBox.Focused && !descriptionTextBox.Focused && !runButton.Focused)
            {
                //Zcb 2021-05-24 19:01
                DisableControl();
            }

            if (!removeButton.Focused)
            {
                removeButton.Enabled = false;
            }
        }

        private void commandsTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {
                if (commandsTreeView.SelectedNode != null)
                {
                    Point pt = new Point(e.X, e.Y);
                    if (!commandsTreeView.SelectedNode.Bounds.Contains(pt))
                    {
                        return;
                    }
                }

                refreshData(false);
            }
        }

        private void commandsTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            TreeViewHitTestInfo treeViewHitTestInfo = commandsTreeView.HitTest(e.Location);
            if (treeViewHitTestInfo.Location != TreeViewHitTestLocations.Label)
            {
                return;
            }

            if (commandsTreeView.SelectedNode != null && commandsTreeView.SelectedNode.GetNodeCount(true) == 0)
            {
                Run();
            }
        }


        private void moreSplitButton_Click(object sender, EventArgs e)
        {
        }

        private void openTempFolderMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void MainForm_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string text = directoryName + "\\" + Resources.HelpFileWithExt;
            if (File.Exists(text))
            {
                Process.Start(text);
                return;
            }

            notesTextBox.Text = Resources.NoCHMFile + directoryName;
        }

        private void ShowStatusLabel(string msg)
        {
            notesTextBox.ForeColor = Color.Black;
            notesTextBox.Text = msg;
            notesTextBox.Refresh();
        }

        private void ShowStatusError(string msg)
        {
            notesTextBox.ForeColor = Color.Red;
            notesTextBox.Text = msg;
            notesTextBox.Refresh();
        }

        private void commandsTreeView_RefreshData(bool bSetCmdTxtBoxTips)
        {
            AddinsCommand addins = m_aim.AddinManager.Commands;
            RefreshTreeView(commandsTreeView, addins, true);

            //下拉提示框
            if (bSetCmdTxtBoxTips)
            {
                nametextBox.AutoCompleteCustomSource.Clear();
                foreach (KeyValuePair<string, Addin> keyValuePair in addins.AddinDict)
                {
                    string key = keyValuePair.Key;
                    Addin addin = keyValuePair.Value;


                    var cmdLst = addin.ItemList;
                    cmdLst.Sort(new ItemComparer());

                    foreach (var item in cmdLst)
                    {
                        var fullClassName = item.FullClassName;

                        string cmdName = GetCmdName(fullClassName);

                        nametextBox.AutoCompleteCustomSource.Add(cmdName);
                    }
                }

                nametextBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
                nametextBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
            }
        }

        public static string GetCmdName(string fullClassName)
        {
            int num = fullClassName.LastIndexOf(".", StringComparison.Ordinal);
            string cmdName = fullClassName.Substring(num + 1);
            return cmdName;
        }

        private void applicationsTreeView_RefreshData()
        {
            RefreshTreeView(applicationsTreeView, m_aim.AddinManager.Applications, true);
        }


        public class ItemComparer : IComparer<AddinItem>
        {
            public int Compare(AddinItem lhv, AddinItem rhv)
            {
                if (lhv != null && rhv != null)
                {
                    return String.Compare(lhv.FullClassName, rhv.FullClassName, StringComparison.Ordinal);
                }
                else
                {
                    return 1;
                }
            }
        }


        private void RefreshTreeView(TreeView tree, Addins addins, bool bTrySelectFirstNode)
        {
            if (addins == null)
            {
                return;
            }

            tree.BeginUpdate();
            tree.Nodes.Clear();
            foreach (KeyValuePair<string, Addin> keyValuePair in addins.AddinDict)
            {
                string key = keyValuePair.Key;
                Addin value = keyValuePair.Value;
                Node node = new Node(key);
                node.Tag = value;
                node.StateImageKey = @"Checked.bmp";

                if (bTrySelectFirstNode)
                {
                    node.Checked = true;
                }


                value.ItemList.Sort(new ItemComparer());

                foreach (AddinItem addinItem in value.ItemList)
                {
                    Node node2 = new Node(addinItem.FullClassName);

                    node2.Tag = addinItem;
                    if (bTrySelectFirstNode)
                    {
                        node2.Checked = true;
                    }

                    node2.StateImageKey = @"Checked.bmp";
                    node.Nodes.Add(node2);
                }

                tree.Nodes.Add(node);
            }

            tree.EndUpdate();

            if (bTrySelectFirstNode)
            {
                if (tree.Nodes.Count > 0)
                {
                    tree.ExpandAll();
                    int index = tree.Nodes.Count - 1;
                    tree.SelectedNode = tree.Nodes[index];
                }

                if (tree.SelectedNode != null)
                {
                    tree.SelectedNode.EnsureVisible();
                }
            }
        }

        private void saveLocalMenuItem_Click(object sender, EventArgs e)
        {
            if (!m_aim.AddinManager.HasItemsToSave())
            {
                MessageBox.Show(Resources.NoItemsSelected, Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            m_aim.AddinManager.SaveToLocal();
        }


        private void saveToAddinMenuItem_Click(object sender, EventArgs e)
        {
            if (!m_aim.AddinManager.HasItemsToSave())
            {
                MessageBox.Show(Resources.NoItemsSelected, Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            string text = m_aim.AddinManager.SaveToAllUserManifest();
            if (!string.IsNullOrEmpty(text) || !File.Exists(text))
            {
                ShowStatusError("Failed to save: " + text);
            }

            ShowStatusLabel("Manifest file saved: " + text);
        }
    }
}